<template>
    <div class="layout">
        <Form ref="formLogin" :model="formLogin" :rules="ruleLogin" :label-width="80">
            <Form-item label="User" prop="user">
                <Input type="text" v-model="formLogin.user" placeholder="Username">
                    <Icon type="ios-person-outline" slot="prepend"></Icon>
                </Input>
            </Form-item>
            <Form-item label="Password" prop="password">
                <Input type="password" v-model="formLogin.password" placeholder="Password">
                    <Icon type="ios-locked-outline" slot="prepend"></Icon>
                </Input>
            </Form-item>
            <Form-item>
                <Button type="primary" @click="handleLogin('formLogin')" :loading="loading">登录</Button>
                <Button type="ghost" @click="handleReset('formLogin')" style="margin-left: 8px">重置</Button>
            </Form-item>
        </Form>
    </div>
</template>
<script>
    export default {
        data () {
            return {
                loading:false,
                formLogin: {
                    username: '',
                    password: ''
                },
                ruleLogin: {
                    user: [
                        { required: true, message: '请填写用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请填写密码', trigger: 'blur' },
                        { type: 'string', min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
                    ]
                },
                
            }
        },
        methods: {
            handleLogin(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.loading = true;
                        this.$store.dispatch('Login', this.formLogin).then(() => {
                            this.loading = false;
                            this.$router.push({ path: '/' })
                        }).catch(err => {
                            this.$Message.error(err);
                            this.loading = false;
                        });
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            handleReset (name) {
                
                this.$refs[name].resetFields();
            }
        }
    }
</script>

<style scoped>
    .layout{
        width:300px;
        margin:auto;
        position:relative;
        top:500;
    }
</style>
